<template>
  <div class="home-category" @mouseleave="categoryId = null">
    <ul class="menu">
      <li
        :class="{ active: categoryId === item.id }"
        v-for="item in list"
        :key="item.id"
        @mouseenter="categoryId = item.id"
      >
        <!-- 一级分类 -->
        <RouterLink to="/">{{ item.name }}</RouterLink>
        <!-- 二级分类 -->
        <template v-if="item.children">
          <RouterLink v-for="sub in item.children" :key="sub.id" to="/">
            {{ sub.name }}
          </RouterLink>
        </template>

        <span v-else>
          <XtxSkeleton
            width="60px"
            height="18px"
            style="margin-right: 5px"
            bg="rgba(255,255,255,0.2)"
          />
          <XtxSkeleton width="50px" height="18px" bg="rgba(255,255,255,0.2)" />
        </span>
      </li>
    </ul>

    <!-- 碳层布局 -->
    <div class="layer">
      <h4>分类推荐 <small>根据您的购买或浏览记录推荐</small></h4>
      <ul v-if="goods && goods.length">
        <li v-for="item in goods" :key="item.id">
          <RouterLink to="/">
            <img v-lazyload="item.picture" alt="" />
            <div class="info">
              <p class="name ellipsis-2">{{ item.name }}</p>
              <p class="desc ellipsis">{{ item.desc }}</p>
              <p class="price"><i>¥</i>{{ item.price }}</p>
            </div>
          </RouterLink>
        </li>
      </ul>

      <!-- 品牌布局 -->
      <ul v-if="brand.brands.length && categoryId === 'brandId'">
        <li class="brand" v-for="item in brand.brands" :key="item.id">
          <RouterLink to="/">
            <img v-lazyload="item.picture" alt="" />
            <div class="info">
              <p class="place">
                <i class="iconfont icon-dingwei"></i>{{ item.place }}
              </p>
              <p class="name ellipsis">{{ item.name }}</p>
              <p class="desc ellipsis-2">{{ item.desc }}</p>
            </div>
          </RouterLink>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { computed, ref, reactive } from 'vue'
import { useStore } from 'vuex'
import { findBrand } from '@/api/home.js'

export default {
  name: 'HomeCategory',
  setup () {
    const store = useStore()
    // 当前鼠标悬停的分类的id
    const categoryId = ref(null)
    // 准备品牌数据
    const brand = reactive({
      id: 'brandId',
      name: '品牌',
      children: [{ id: 'subBrandId', name: '品牌推荐' }],
      // 品牌数据
      brands: []
    })

    // 把二级分类处理成两个
    const list = computed(() => {
      const cates = store.getters.cates.map((item) => {
        // 把item对象中的每一个属性复制一份放到obj对象中
        const obj = { ...item }
        obj.children = obj.children && obj.children.slice(0, 2)
        return obj
      })
      // 添加品牌数据
      cates.push(brand)
      return cates
    })

    // 根据当前一级分类的id，获取右侧碳层的商品数据
    const goods = computed(() => {
      const cate = store.getters.cates.find(
        (item) => item.id === categoryId.value
      )
      return cate ? cate.goods : []
    })
    // 获取品牌的数据
    findBrand()
      .then((res) => {
        brand.brands = res.result
      })
      .catch(() => {
        alert('获取品牌数据失败')
      })

    return { list, categoryId, goods, brand }
  }
}
</script>

<style scoped lang='less'>
.home-category {
  width: 250px;
  height: 500px;
  background: rgba(0, 0, 0, 0.8);
  position: relative;
  z-index: 99;
  .xtx-skeleton {
    animation: fade 1s linear infinite alternate;
  }
  @keyframes fade {
    from {
      opacity: 0.2;
    }
    to {
      opacity: 1;
    }
  }
  .menu {
    li {
      padding-left: 40px;
      height: 50px;
      line-height: 50px;
      &:hover,
      &.active {
        background: @xtxColor;
      }
      a {
        margin-right: 4px;
        color: #fff;
        &:first-child {
          font-size: 16px;
        }
      }
    }
  }
  .layer {
    width: 990px;
    height: 500px;
    background: rgba(255, 255, 255, 0.8);
    position: absolute;
    left: 250px;
    top: 0;
    display: none;
    padding: 0 15px;
    h4 {
      font-size: 20px;
      font-weight: normal;
      line-height: 80px;
      small {
        font-size: 16px;
        color: #666;
      }
    }
    ul {
      display: flex;
      flex-wrap: wrap;
      li {
        width: 310px;
        height: 120px;
        margin-right: 15px;
        margin-bottom: 15px;
        border: 1px solid #eee;
        border-radius: 4px;
        background: #fff;
        &:nth-child(3n) {
          margin-right: 0;
        }
        a {
          display: flex;
          width: 100%;
          height: 100%;
          align-items: center;
          padding: 10px;
          &:hover {
            background: #e3f9f4;
          }
          img {
            width: 95px;
            height: 95px;
          }
          .info {
            padding-left: 10px;
            line-height: 24px;
            width: 190px;
            .name {
              font-size: 16px;
              color: #666;
            }
            .desc {
              color: #999;
            }
            .price {
              font-size: 22px;
              color: @priceColor;
              i {
                font-size: 16px;
              }
            }
          }
        }
      }
      li.brand {
        height: 180px;
        a {
          align-items: flex-start;
          img {
            width: 120px;
            height: 160px;
          }
          .info {
            p {
              margin-top: 8px;
            }
            .place {
              color: #999;
            }
          }
        }
      }
    }
  }
  &:hover {
    .layer {
      display: block;
    }
  }
}
</style>
